package naga.x.platform2.channel.jg.support;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

import com.alibaba.fastjson.JSONObject;

import naga.x.platform2.app.model.RechargeTemplate;
import naga.x.platform2.channel.s399.support.S399Api.Code;
import naga.x.platform2.channel.s399.support.S399Api.PayInfo;
import naga.x.platform2.utils.CommonUtils;
import naga.x.platform2.utils.IOUtils;
import naga.x.platform2.utils.NumberUtils;
import naga.x.platform2.utils.StringUtils;
import naga.x.platform2.utils.UUID;

@Component
public class JGApi {

	@Autowired
	private JGConfig config;
	
	public String createLoginRedirectUrl(JSONObject user) {
//		String gameid, String channel, String token ,uid,nickname,sex,headimgurl,code
		StringBuilder buf = new StringBuilder(UrlBasedViewResolver.REDIRECT_URL_PREFIX);
		buf.append(config.getClientUrl());
		Integer code = user.getInteger("errcode");
		if (code == null) { // succ
			buf.append("&code=0")
				.append("&gameid=").append(config.getGameId())
				.append("&channel=").append(user.getString("channel"))
				.append("&token=").append(user.getString("token"))
				.append("&uid=").append(user.getString("uid"))
				.append("&nickname=").append(CommonUtils.encodeURL(user.getString("nickname"), IOUtils.UTF8))
				.append("&sex=").append(user.getIntValue("sex"))
				.append("&headimgurl=").append(CommonUtils.encodeURL(user.getString("headimgurl"), IOUtils.UTF8));
		} else {
			buf.append("&code=").append(code)
				.append("msg=").append(user.getString("errmsg"));
		}
		return buf.toString();
	}
	
	public Object newError(Code code) {
		JSONObject error = new JSONObject(2);
		error.put("code", code.code);
		error.put("msg", code.msg);
		return error;
	}
	
	public Object newOrder(Integer serverId, Long playerId, RechargeTemplate product) {
//		orderid: 商户订单号，string，必要
//		money: 订单金额（单位：分），int，必要
//		product: 商品名称，string，必要
//		spid: 商户ID，string，必要
//		sign: 签名，string，必要
//		attach: 附加参数，在支付回调原样返回，string，可选
		JSONObject order = new JSONObject(8);
		String orderId = UUID.randomUUID().toString();
		order.put("code", Code.SUCC.code);
		order.put("orderid", orderId);
		order.put("money", product.getPrice() * 100);
		order.put("product", product.getName());
		order.put("spid", config.getSpId());
		order.put("sign", makeOrderSign(orderId, product));
		order.put("attach", serverId + "|" + playerId + "|" + product.getId());
		return order;
	}
	
	public String makeOrderSign(String orderId, RechargeTemplate product) {
//		sign = MD5.encode(orderid + "" + money + "" + spid + "" + spkey)
		return CommonUtils.md5(orderId + (product.getPrice() * 100) + config.getSpId() + config.getSpKey());
	}
	
	public String makePayCallbackSign(String orderId, Integer money, Integer status) {
//		sign = MD5.encode(orderid + "" + money + "" + status + "" + spkey)
		return CommonUtils.md5(orderId + money + status + config.getSpKey());
	}
	
	public String makeSign(String... params) {
		StringBuilder builder = new StringBuilder(32);
		for (String param : params) {
			builder.append(param);
		}
		builder.append(config.getSpKey());
		return CommonUtils.md5(builder.toString());
	}
	
	public static PayInfo parsePay(String attach) {
		String[] strs = StringUtils.split(attach, "|");
		if (strs == null || strs.length != 3) {
			return PayInfo.EMPTY;
		}
		int serverId = NumberUtils.parseInt(strs[0]);
		long playerId = NumberUtils.parseLong(strs[1]);
		int productId = NumberUtils.parseInt(strs[2]);
		return new PayInfo(serverId, playerId, productId);
	}
	
}
